#!/bin/bash

tst=""
[ "$1" == "test" ] && tst="echo"

cur_dir="`pwd`"
cd "`dirname $0`/../"
svn_conf="`pwd`"
cd "$cur_dir"

function link_spe(){
  src="$1"
  tgt="$2"
  mod="$3"
  [ -h "$tgt" ]         && $tst unlink $tgt
  [ -w "$tgt" -a "$mod" == "hard" ] && $tst rm -rf $tgt
  [ -d "$tgt" ]         && $tst cp -r $tgt/* $src/
  [ -f "$tgt" ]         && $tst cp $tgt $src
  [ -w "$tgt" ]         && $tst mv $tgt /tmp/trash
  [ "$mod" != "hard" ]  && $tst ln -s $src $tgt
  [ "$mod" == "hard" ]  && $tst cp -al $src $tgt
  [ "$tst" != "" ]      && echo ""
}

function link_any(){
  tgt="$1"
  p2=`echo $1 | awk 'BEGIN{homed="'"$HOME/"'"}{sub(homed, "/home/", $0); print $0}'`
  src="$svn_conf$p2"
  link_spe "$src" "$tgt" "$2"
}

function link_conf(){
  [ ! -d /tmp/trash ] && mkdir /tmp/trash

  link_spe "`dirname $svn_conf`/scripts" "$HOME/scripts"

  for fn in "$HOME/.ssh" "$HOME/.subversion" \
  /etc/apache2 /etc/openvpn /etc/samba /etc/squid3 \
  /etc/exports /etc/rc.local /etc/rsyncd.conf /etc/hosts
  do
    link_any "$fn"
  done

  link_any /etc/bind "hard"
}

function set_perm(){
  chmod 700 "$svn_conf/home"
  chmod 600 "$svn_conf/home/.ssh/*" 
  chmod 700 $0
}

set_perm
link_conf

